package com.lstudio.mediademo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'media-jni-lib' library on application startup.
    static {
        System.loadLibrary("media-jni-lib");
    }

    String mp4file = "/data/local/tmp/testfile.mp4";

    SurfaceView mSurfaceView;
    SurfaceHolder mSurfaceHolder;
    Button mPlayBtn;
    static int PLAY_STATUS_STOP = 0;
    static int PLAY_STATUS_PLAYING = 1;
    int mPlayStatus;//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI()+getMediaJNIVersion());

        mSurfaceView = findViewById(R.id.surfaceView);
        mSurfaceHolder = mSurfaceView.getHolder();
        //mSurfaceView.setZOrderOnTop(true);
        //mSurfaceView.setZOrderMediaOverlay(true);
        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });

        mPlayStatus = PLAY_STATUS_STOP;
        mPlayBtn = findViewById(R.id.startPlay);
        mPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start play
                if (getPlayerStatus() == PLAY_STATUS_STOP) {
                    playVideo(mp4file, mSurfaceHolder.getSurface());
                }
                else {
                    stopPlayVideo();
                }
            }
        });
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
    public native String getMediaJNIVersion();
    public native int playVideo(String filepath, Surface s);
    public native int stopPlayVideo();
    public native int getPlayerStatus();
}